﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OBUActivation.Tags;

namespace OBUActivation.Tags
{
    class TransAndRetrans
    {

        /// <summary>
        /// 对数据进行转义
        /// </summary>
        /// <param name="buff">字节数组</param>
        /// <param name="len">数组长度</param>
        /// <returns>转义后数组</returns>
        public static byte[] TransCode(byte[] buff, int len)
        {
            List<byte> tmpbuf = new List<byte>();
            for (int i = 0; i < len; i++)
            {
                byte ch = buff[i];
                if (ch == 0XFF)
                {
                    tmpbuf.Add(0XFE);
                    tmpbuf.Add(0x01);
                }
                else if (ch == 0XFE)
                {
                    tmpbuf.Add(0XFE);
                    tmpbuf.Add(0x00);
                }
                else
                {
                    tmpbuf.Add(ch);
                }
            }

            byte[] retbuff = new byte[tmpbuf.Count];
            tmpbuf.CopyTo(retbuff);
            return retbuff;
        }

        /// <summary>
        /// 对字节数组去掉报文头和报文尾，然后进行反转义
        /// </summary>
        /// <param name="buff">字节数组</param>
        /// <param name="len">数组长度</param>
        /// <returns>反转义后数组</returns>
        public static byte[] ReTransCode(byte[] buff, int len)
        {
            int pos = 2;
            List<byte> tmpbuf = new List<byte>();
            while (pos < len - 1)
            {
                if (buff[pos] == 0XFE)
                {
                    if (buff[pos + 1] == 0x00)
                    {
                        tmpbuf.Add(0XFE);
                    }
                    else
                    {
                        tmpbuf.Add(0XFF);
                    }
                    pos += 2;
                }
                else
                {
                    tmpbuf.Add(buff[pos++]);
                }
            }

            byte[] retbuff = new byte[tmpbuf.Count];
            tmpbuf.CopyTo(retbuff);
            return retbuff;
        }


        //计算CRC校验码
        public static byte ComputeCRC(byte[] buff, int len)
        {
            byte mysum = 0;
            for (int i = 0; i < len; i++)
            {
                mysum = (byte)(mysum ^ buff[i]);
            }
            return mysum;
        }


        //添加CRC校验码
        public static byte[] AddCRC(byte[] buff, int len)
        {
            List<byte> msg = new List<byte>();
            byte crc = ComputeCRC(buff, buff.Length);
            msg.AddRange(buff);
            msg.Add(crc);

            return msg.ToArray();
        }

        /*
        //检验FCS校验码是否正确
        public static bool CheckFCS(byte[] data, int len)
        {
            if (!CRC.DataCRC(data, (byte)data.Length))
                return false;
            return true;
        }
        

        //添加FCS校验码
        public static byte[] AddFCS(byte[] buff, int len)
        {
            byte[] crc = new byte[buff.Length + 2];
            Array.Copy(buff, 0, crc, 0, len);
            CRC.ConCRC(ref crc, (byte)len);
            return crc;
        }

    */

        //检验CRC校验码是否正确
        public static bool IsCRCCorrect(byte[] msg)
        {
            if (msg[msg.Length - 1] == ComputeCRC(msg, msg.Length - 1))
            {
                return true;
            }
            return false;
        }
    }
    
}
